﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace EVEMap
{

    /// <summary>
    /// A subset of the information from the EvEAI.Asset class, representing one stack of
    /// something at a station.
    /// </summary>
    public class assetStack
    {
        long itemID;
        int typeID;
        int solarSystemID;
        int stationID;
        int quantity;
        double value;

        public assetStack(System.Data.DataRow row)
        {
            solarSystemID = Convert.ToInt32(row["SolarSystemID"]);
            itemID = Convert.ToInt64(row["ItemID"]);
            typeID = Convert.ToInt32(row["TypeID"]);
            stationID = Convert.ToInt32(row["StationID"]);
            quantity = Convert.ToInt32(row["Quantity"]);
            value = Convert.ToDouble(row["Value"]);
        }

        public assetStack(assetStack other)
        {
            solarSystemID = other.solarSystemID;
            itemID = other.itemID;
            typeID = other.typeID;
            stationID = other.stationID;
            quantity = other.quantity;
            value = other.value;
        }

        public double Value
        {
            get
            {
                return value;
            }
        }
    }

    /// <summary>
    /// represents a list of assetStacks, the route to coalesce them with another station's assets,
    /// and the starting and ending stations
    /// </summary>
    public class coalesceAssetsList
    {
        #region members
        List<assetStack> theAssets;
        List<int> coalesceRoute;
        //int startingStation, endingStation;
        //string startStationName, endStationName;
        eveStation startStation, endStation;
        #endregion

        public coalesceAssetsList(eveStation Station)
        {
            theAssets = new List<assetStack>(Station.Assets);
            coalesceRoute = new List<int>();
            startStation = Station;
        }

        /// <summary>
        /// Add the given gate jump to the route
        /// </summary>
        /// <param name="jump">id of system being added</param>
        public void AddJump(int jump)
        {
            coalesceRoute.Add(jump);
        }

        public void Dock(eveStation station)
        {
            endStation = station;
            // todo - move the assets to end station?
        }

        public override string ToString() 
        {
            string retval;
            if (endStation != null)
                retval = string.Format("{0}\t{1}\t\t{2}\t\t{3:0,0}", coalesceRoute.Count, startStation.Name, endStation.Name, Collateral);
            else
                retval = string.Format("{0}\t\t{1:0,0}", startStation.Name, Collateral);
            return retval;
        }

        /// <summary>
        /// Starting station ID (where the assets came from)
        /// </summary>
        public int coalesceBegin
        {
            get
            {
                return startStation.ID;
            }
        }

        /// <summary>
        /// Ending station ID. Only valid after Dock()
        /// </summary>
        public int coalesceEnd
        {
            get
            {
                return endStation.ID;
            }
        }

        /// <summary>
        /// Printable name of originating station
        /// </summary>
        public string StartName
        {
            get { return startStation.Name; }
        }

        /// <summary>
        /// Printable name of destination station. Only valid after Dock()
        /// </summary>
        public string EndName
        {
            get { return endStation.Name; }
        }

        /// <summary>
        /// Sum of asset values.
        /// </summary>
        public double Collateral
        {
            get
            {
                return theAssets.Sum(item => item.Value);
                //double retval = 0;
                //foreach (assetStack stack in theAssets)
                //    retval += stack.Value;
                //return retval;
            }
        }

        /// <summary>
        /// Length of the route from originating station to destination station.
        /// </summary>
        public int Jumps
        {
            get
            {
                return coalesceRoute.Count;
            }
        }

    }


}